package com.example.wifimonitor.com.hanj;

import cn.hutool.core.util.URLUtil;
import cn.hutool.json.JSONObject;

import java.util.ArrayList;

/**
 * @author hanj.cn@outlook.com
 */
public class MsgHandler implements Runnable {

    @Override
    public void run() {

        // 计数
        long counter = 0;

        // 休眠时间
        int eventInterval = 60 * 1000;

        // 接收响应
        ArrayList<JSONObject> response;

        // 接收hostName
        ArrayList<String> hostNames = new ArrayList<>();

        while (true) {

            // counter重置
            if (counter == Long.MAX_VALUE) {
                counter = 0L;
            }

            System.out.println("--- 第" + (++counter) + "次查询 ---");

            // 查询
            response = MyUtils.getMercuryResponse();

            if (response == null) {
                System.out.println("此次循环跳过");
                // 发送消息
                SendMessage.sendMsgToBark("stok失效", URLUtil.encode("❗❗❗stok已失效"), "", null);
                continue;
            }

            // 存放所有hostName
            for (JSONObject jsonObject : response) {

                hostNames.add(jsonObject.get("hostname", String.class));
            }

            // 比对两次hostNames的内容
            MyUtils.handleHostNames(hostNames);

            //hostNames清零
            hostNames.clear();

            // 休眠
            try {
                Thread.sleep(eventInterval);
            } catch (InterruptedException e) {
                e.printStackTrace();

            }
        }

    }

}
